package cn.fl.customer.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.customer.service.ICstCertInfoService;
import cn.fl.customer.transform.CstCertRecVOTransform;
import cn.fl.customer.vo.CstCertRecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.DictionaryConstant;
import fl.customer.facade.customer.api.ICustomerAddApiService;
import fl.customer.facade.customer.api.ICustomerModifyApiService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.api.ICustomerRemoveApiService;
import fl.customer.facade.customer.condition.CstCertRecQC;
import fl.customer.facade.customer.dto.CstCertRecDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/5.
 */
@Service
public class CstCertInfoServiceImpl implements ICstCertInfoService {

    private static final Logger logger = LoggerFactory.getLogger(CstCertInfoServiceImpl.class);

    @Autowired
    private ICustomerQueryApiService customerQueryApiService;

    @Autowired
    private ICustomerAddApiService customerAddApiService;

    @Autowired
    private ICustomerRemoveApiService customerRemoveApiService;

    @Autowired
    private ICustomerModifyApiService customerModifyApiService;

    @Autowired
    private ISysUsrApiService sysUsrApiService;

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Override
    public Pager searchListPage(Pager page) {
        if (null == page) page = new Pager();
        Pager pager = this.customerQueryApiService.searchCstCertRecListPage(page);
        List<CstCertRecDTO> resultList = (List<CstCertRecDTO>) pager.getResultList();
        //获取创建人列表和证件类型
        if (CollectionUtils.isNotEmpty(resultList)) {
            List<Long> usrIdList = new ArrayList<>(resultList.size());
            for (CstCertRecDTO cstCertRecDTO : resultList) {
                if (usrIdList.contains(cstCertRecDTO.getCrtUsrId())) {
                    continue;
                }
                usrIdList.add(cstCertRecDTO.getCrtUsrId());
            }
            SysUsrQueryCondition sysUsrQueryCondition = new SysUsrQueryCondition();
            sysUsrQueryCondition.setUsrIds(usrIdList);
            sysUsrQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
            List<SysUsrDTO> usrList = sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
            Map<Long, String> usrMap = null;
            if (CollectionUtils.isNotEmpty(usrList)) {
                usrMap = new HashMap<>(usrList.size());
                for (SysUsrDTO dto : usrList) {
                    usrMap.put(dto.getId(), dto.getRefRlNm());
                }
            }
            //证件类型
            String codeGrpCd = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
            List<KeyValueVO> keyValueVOList = this.sysDictionaryApiService.getKvList(codeGrpCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
            Map<String, String> certTypMap = null;
            if (CollectionUtils.isNotEmpty(keyValueVOList)) {
                certTypMap = new HashMap<>(keyValueVOList.size());
                for (KeyValueVO dto : keyValueVOList) {
                    certTypMap.put(dto.getKey(), dto.getValue());
                }
            }
            for (CstCertRecDTO cstCertRecDTO : resultList) {
                if (usrMap != null && usrMap.containsKey(cstCertRecDTO.getCrtUsrId())) {
                    cstCertRecDTO.setCrtUsrName(usrMap.get(cstCertRecDTO.getCrtUsrId()));
                }
                if (certTypMap != null && certTypMap.containsKey(cstCertRecDTO.getCertTypCd())) {
                    cstCertRecDTO.setCertTypCd(certTypMap.get(cstCertRecDTO.getCertTypCd()));
                }
            }
        }
        pager.setResultList(CstCertRecVOTransform.toVOList(resultList));
        return pager;
    }

    @Override
    public Long add(CstCertRecVO entity) {
        if (null == entity) {
            BaseLogUtils.info(logger, "add", "新增客户证件记录表信息失败,客户证件记录表对象参数为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        CstCertRecQC qc = new CstCertRecQC();
        qc.setCertNo(entity.getCertNo());
        qc.setCertTypCd(entity.getCertTypCd());
        List<CstCertRecDTO> list = this.customerQueryApiService.searchCstCertRecList(qc);
        if (list.size() > 0) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_606, "证件已存在!");
        }
        return this.customerAddApiService.addCstCertRec(CstCertRecVOTransform.toDTO(entity));
    }

    @Override
    public Boolean modify(CstCertRecVO entity) {
        if (null == entity) {
            BaseLogUtils.info(logger, "modify", "修改客户证件记录表信息失败,客户证件记录表对象参数为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        CstCertRecQC qc = new CstCertRecQC();
        qc.setCertNo(entity.getCertNo());
        qc.setCertTypCd(entity.getCertTypCd());
        List<CstCertRecDTO> list = this.customerQueryApiService.searchCstCertRecList(qc);
        if (list.size() > 0) {
            for (CstCertRecDTO cstCertRecDTO : list) {
                if (!entity.getId().equals(cstCertRecDTO.getId())) {
                    throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_606, "证件已存在!");
                }
            }
        }
        return this.customerModifyApiService.modifyCstCertRec(CstCertRecVOTransform.toDTO(entity));
    }

    @Override
    public Boolean removeById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "removeById", "删除客户证件记录表信息失败,客户证件记录表对象参数为空.DATA:" + id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.customerRemoveApiService.removeCstCertRec(id);
    }

    @Override
    public CstCertRecVO selectById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "selectById", "修改客户证件记录表信息时查询一条数据失败,客户证件记录表对象参数为空.DATA:" + JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        CstCertRecDTO dto = this.customerQueryApiService.searchCstCertRecById(id);
        return CstCertRecVOTransform.toVO(dto);
    }
}
